package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.config.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-11
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //注入VideoService
    @Autowired
    private EduVideoService videoService;

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String couresId) {
        //根据课程ID查询所有章节的数据
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",couresId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //根据课程ID查询所有章节的数据
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",couresId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建list，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();
        //遍历查询章节的list集合
        for (int i = 0; i < eduChapterList.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter chapter = eduChapterList.get(i);
            BeanUtils.copyProperties(chapter,chapterVo);

            //遍历查询小节的list集合
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int i1 = 0; i1 < eduVideoList.size(); i1++) {
                //通过章节ID获取小节信息
                EduVideo video =eduVideoList.get(i1);
                if (video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            finalList.add(chapterVo);
        }
        return finalList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节Id查询小节表，判断该章节是否存在小节，存在就不删掉
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断
        if (count>0){
            throw new GuliException(20001,"不能删除");
        }else {
            int result = baseMapper.deleteById(chapterId);
            //成功 1>0 true 0>0 fales
            return result>0;
        }
    }

    //删除章节根据课程ID
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
